
<template>
  <el-dialog v-model="dialogFormVisible" class="dialog-title" title="发送邮件" width="70%">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px">
      <el-form-item label="Email" prop="email">
        <el-input
          v-model="temp.email"
          placeholder="Email，多个以英文逗号“,”隔开"
        />
      </el-form-item>
      <el-form-item label="模版编码" prop="code">
        <el-select v-model="temp.code" class="filter-item" placeholder="Please select" style="width: 100%" @change="handleSelect">
          <el-option v-for="item in templateList" :key="item.id" :label="item.name + '(' + item.code + ')'" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="变量值" prop="data">
        模板变量的json字符串
        <el-input ref="jsonEditor" v-model="temp.data" type="textarea" />
        模板内容:{{ template.templateContent }}
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button type="primary" :loading="sendEmailLoading" @click="runSendEmail">发送邮件</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { sendEmail } from '@/api/email/sendlog'
import { unescape } from '@/utils'
import { fetchTemplateList } from '@/api/email/template'
const jsonData = '{}'
export default {
  name: 'SendLog',
  emits: ['refreshList'],
  data() {
    return {
      templateList: undefined,
      rules: {
        email: [{ required: true, message: 'Email地址必填' }],
        code: [{ required: true, message: '编码必填' }]
      },
      template: {},
      dialogFormVisible: false,
      sendEmailLoading: false,
      temp: {
        phone: '',
        code: '',
        data: JSON.parse(jsonData)
      }
    }
  },
  created() {
    this.getTemplateList()
  },
  methods: {
    /**
     * 刷新列表
     */
    refreshList() {
      this.$emit('refreshList')
    },
    /**
     * 获取模版编码
     */
    getTemplateList() {
      this.listLoading = true
      fetchTemplateList(this.listQuery).then(response => {
        this.templateList = response.data.data
      })
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.sendEmailLoading = false
      this.temp = {
        phone: '',
        code: '',
        data: JSON.parse(jsonData)
      }
    },
    /**
     * 提交新增
     */
    handleSendEmail() {
      this.resetTemp()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 发送邮件
     */
    runSendEmail() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.sendEmailLoading = true
          sendEmail(this.temp).then(response => {
            this.sendEmailLoading = false
            if (response.data.success) {
              this.dialogFormVisible = false
              this.refreshList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    /**
     *
     * @param val 模板id
     */
    handleSelect(val) {
      this.templateList.forEach(item => {
        if (item.code === val) {
          this.template = item
          // console.log('this.template', this.template)
          this.template.templateContent = unescape(this.template.templateContent)
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
